/**
 * fileName:  AccountPersistence.java
 * author:  Lei
 * Company:  Cloudland Studio
 * Createdate:  2014-2-22 下午4:26:04
 *
 * All rights Reserved, Designed By cloudland Copyright(C) 2010-2020
 *   
 * Modification  History:
 *     Date           Author        Version        
 * -------------------------------------------
 * 2014-2-22          Lei            1.0        
 *
 * Why & What is modified:
 * 1.0: 文件创建
 */
package org.cloudland.commercial.uphold.store.persisten;

import org.apache.log4j.Logger;
import org.cloudland.commercial.uphold.store.IPersistence;
import org.cloudland.commercial.uphold.store.entity.AccountEntity;
import org.cloudland.udap.database.Persistence;
import org.cloudland.udap.database.entity.ManagerEntity;
import org.cloudland.util.Randoms;
import org.cloudland.util.Securitys;

/**
 * <pre>
 * TODO 请充分描述该类提供的作用, 或与其他类会有关联的使用。
 * 使用 java.lang.Object#toString(), 或者 {@link java.lang.Character Character} 等标签
 * </pre>
 * 
 * @ClassName AccountPersistence
 * @author Lei
 * @version %I%, %G%
 * @see
 * @since JDK1.6
 * 
 */
public class AccountPersistence implements IPersistence<AccountEntity> {
	
	/**
	 * LOGGER 日志对象
	 */
	private Logger LOGGER = Logger.getLogger(AccountPersistence.class);
	
	/**
	 * access 持久化操作对象
	 */
	private Persistence<ManagerEntity> access;
	
	public AccountPersistence(Persistence<ManagerEntity> persistence) {
		this.access = persistence;
	}
	
	/**
	 * 新增账号信息
	 * @param entity 账号实体对象
	 * @return 新增账号的标示符
	 */
	public void save(AccountEntity entity) {
		if (LOGGER.isDebugEnabled()) {
			LOGGER.debug("新增后台管理员账号。" + entity);
		}
		
		ManagerEntity en = new ManagerEntity();
		en.setId(Randoms.uuid());
		en.setName(entity.getName());
		
		// 密码加盐处理
		Integer salt = Randoms.nextInt(100000, 999999);
		en.setSalt(salt.toString());
		String passwd = Securitys.doMD5(entity.getPasswd().concat(en.getSalt()));
		en.setPassword(passwd);
		
		en.setRoleId(entity.getAuthorizeId());
		access.add(en);
		
		if (LOGGER.isDebugEnabled()) {
			LOGGER.debug("持久化对象：" + en);
		}
		
	}

	/**
	 * 更新账号信息
	 * @param entity 账号实体对象
	 */
	public void update(AccountEntity entity) {
		
	}

	/**
	 * 删除账号信息
	 * @param entity 账号实体对象
	 */
	public void delete(AccountEntity entity) {
		
	}

}
